package cn.ecut.servlet;

import cn.ecut.model.User;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/getUserInfo")
public class GetUserInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object u = request.getSession().getAttribute("user");
        Map<String ,Object> map = new HashMap<>();
        String msg="";
        if (u instanceof User){
            User user = (User) u;
            msg="获取成功";
            map.put("msg",msg);
            map.put("user",user);
            map.put("status",0);
        }else {
            msg= "获取失败";
            map.put("msg",msg);
            map.put("user",null);
            map.put("status",-1);
        }

        response.getWriter().write(JSON.toJSONString(map));
    }
}
